package com.thingsgrid.device.feign;

import com.thingsgrid.device.entity.Device;
import com.thingsgrid.device.service.IDeviceService;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ApiIgnore
@RestController
//@AllArgsConstructor
@ComponentScan("com.springblade.device.service")
public class DeviceClient implements IDeviceClient {
	@Autowired
	private IDeviceService service;
	@Override
	@GetMapping(API_PREFIX+"/detail2")
	public R<Map<String, Object>> findDeviceById(@PathVariable(value = "deviceId") String deviceId) {
		return R.data(service.findDeviceById(deviceId));
	}

	/**
	 * 查询设备码是否存在
	 * @param deviceSn
	 * @return
	 * 09.16
	 */
	@Override
	@GetMapping(API_PREFIX+"/findDeviceByDeviceSn")
	public Map<String, Object> findDeviceByDeviceSn(String deviceSn) {
		Device device = service.findDeviceByDeviceSn(deviceSn);
		Map<String, Object> map = new HashMap<>();
		if(device!=null) {
			map.put("device", device);
			map.put("state","exist");
		}else{
			map.put("device",null);
			map.put("state","none");
		}
		return map;
	}

	/**
	 *说明：通过产品id获取设备列表
	 *@author lilongfei
	 *@since 2019/9/27 14:18
	 */
	@Override
	public R<List<Device>> getDeviceListByProductId(String productId) {
		if(StringUtils.isEmpty(productId)){
			throw new ServiceException("产品ID不能为空");
		}
		return R.data(service.getDeviceListByProductId(productId));
	}
}
